package com.codewsq.exer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class TestMap {
    /**
     * list转map
     * @author codewsq
     * @date 2022/4/26 0026 17:08
     */
    public static void main(String[] args) {
        TestMap testMap = new TestMap();
        testMap.practice();
    }

    void practice()
    {

        TestMap.Pserson person1 = new Pserson();
        person1.setAge(12);
        person1.setName("Nipples");
        TestMap.Pserson person2 = new Pserson();
        person2.setAge(16);
        person2.setName("feet");
        TestMap.Pserson person3 = new Pserson();
        person3.setAge(17);
        person3.setName("feet");

        List<Pserson> list = new ArrayList<>();
        list.add(person1);
        list.add(person2);
        list.add(person3);

        //  这里进行转Map的操作
        Map<Integer, String> stringMap = list.stream().collect(Collectors.toMap(Pserson::getAge, Pserson::getName,
                (p1, p2) -> p2));
        //2. 这里其实已经完成了，如果要按某种条件分组 相同条件的元素放在一起，就按下面操作
        Map<String, List<Pserson>> collect = list.stream().collect(Collectors.groupingBy(Pserson::getName));
        //3.再续
        System.out.println();
    }
    class Pserson
    {
        private String name;
        private int age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }
}
